//
//  ZYSpecialController.m
//  Project_B
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 ZY. All rights reserved.
//

#import "ZYSpecialController.h"
#import "ZYPopularCollectionViewCell.h"
#import "ZYCollectionReusableView.h"
#import "ZYDetailWebViewController.h"
@interface ZYSpecialController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView *myCollectionView;

@property(nonatomic,strong)NSMutableArray *sectionArr;
@property(nonatomic,strong)NSMutableArray *itemArr;
@property(nonatomic,strong)NSMutableDictionary *allItemDic;
@end

@implementation ZYSpecialController

#pragma mark - 将要出现时
-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHue:0 saturation:0.74 brightness:0.91 alpha:1];
    self.tabBarController.tabBar.hidden = YES;
}

#pragma mark - 将要消失时
-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    
}



#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    
#pragma mark 返回按钮
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 30*ADAPT_WIDTH, 30*ADAPT_HEIGHT);
    [leftBtn setImage:[UIImage imageNamed:@"iconfont-fanhui-bai.png"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    
    
    
    self.sectionArr = [NSMutableArray array];
    self.allItemDic = [NSMutableDictionary dictionary];
    
    self.navigationItem.title = self.navBarTitle;
    
    
    [self makeCollectionView];
    
    [self getData];
    
}


#pragma mark - 返回按钮
-(void)leftBtnAction{
    self.navigationItem.hidesBackButton = YES;
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 创建CollectionView
-(void)makeCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    //头部 大小
    flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH/10);
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, HEADERIMG_HEIGHT + SPACE*2);

    
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) collectionViewLayout:flowLayout];
    [self.view addSubview:self.myCollectionView];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    
    [self.myCollectionView registerClass:[ZYPopularCollectionViewCell class] forCellWithReuseIdentifier:@"ZYPopularCollectionViewCell"];
    //collectionView 的Header Footer 同cell一样遵循重用机制
    [self.myCollectionView registerClass:[ZYCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZYCollectionReusableView"];
    
    
   
}


#pragma mark - 请求数据
-(void)getData{
    
    NSString *url = [NSString stringWithFormat:@"http://c.m.163.com/nc/special/%@.html",self.specialId];
    
    [ZYTools getUrl:url body:nil result:ZYJSON headerFile:nil success:^(id result) {
        
        NSMutableDictionary *Bigdic = [result objectForKey:self.specialId];
        
        self.sectionArr = [NSMutableArray arrayWithArray:[Bigdic objectForKey:@"topics"]];
        
        
        [self.myCollectionView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
}



#pragma mark - collectionView 区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.sectionArr.count;
}



#pragma mark - collectionView 行数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    NSMutableArray *itemArr = [NSMutableArray array];
    NSMutableArray *array = [NSMutableArray array];
    array = [[self.sectionArr objectAtIndex:section] objectForKey:@"docs"];
    
    
    for (NSMutableDictionary *dic in array) {
        ZYSpecialModel *model = [[ZYSpecialModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [itemArr addObject:model];
    }
    
    
    [self.allItemDic setObject:itemArr forKey:[NSString stringWithFormat:@"%ld",section]];
   
    
    return itemArr.count;
    
}


#pragma mark - collectionView 设置cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ZYPopularCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZYPopularCollectionViewCell" forIndexPath:indexPath];
    
     ZYSpecialModel *model = [[ZYSpecialModel alloc]init];
    NSMutableArray *array = [self.allItemDic objectForKey:[NSString stringWithFormat:@"%ld",indexPath.section]];
     model = [array objectAtIndex:indexPath.row];
   
    cell.model = model;

    return cell;
}



#pragma mark - collectionView 设置区头
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    if (kind == UICollectionElementKindSectionHeader) {
        ZYCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZYCollectionReusableView" forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0.9 alpha:1];
        
       reusableView.string = [[self.sectionArr objectAtIndex:indexPath.section] objectForKey:@"tname"];
        
        return reusableView;
    }
    
    return nil;
}


#pragma mark - collectionView 设置item大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(SCREEN_WIDTH, HEADERIMG_HEIGHT + SPACE*2);
}



#pragma mark - collectionView cell触摸方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ZYSpecialModel *model = [[ZYSpecialModel alloc]init];
    NSMutableArray *array = [self.allItemDic objectForKey:[NSString stringWithFormat:@"%ld",indexPath.section]];
    model = [array objectAtIndex:indexPath.row];

    
    ZYDetailWebViewController *detailWebView = [[ZYDetailWebViewController alloc]init];
    detailWebView.docId = model.docid;
    [self.navigationController pushViewController:detailWebView animated:YES];
    
}




#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
